Go에서는 컬렉션 컬렉션은 동일한 타입의 항목들로 구성된 기본적인 그룹으로, 효율적인 접근을 위해 조직됩니다. '화물창'은 별도의 변수가 아니라 하나의 식별자 아래에서 대량의 데이터를 관리해야 한다는 우리의 필요를 나타냅니다.
1. 복합 리터럴
복합 리터럴은 복합 리터럴 사용자가 원하는 값으로 어떤 복합 타입을 초기화하기 위한 간결한 문법입니다. 다음 구문을 사용해 배열을 한 번에 선언하고 초기화할 수 있습니다: 타입{값1, 값2, ...}.
2. 0 기반 탐색
배열은 0부터 인덱스가 시작됩니다. 8개의 행성 컬렉션은 인덱스 0부터 7까지로 접근합니다. 이 범위 밖의 인덱스에 접근하면 컴파일 시 오류 또는 런타임 패닉이 발생합니다.
그림 16.1: 인덱스 0~7을 가진 행성들
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary purpose of a composite literal in Go?
To perform complex mathematical calculations on groups of numbers.
A concise syntax to declare and initialize a composite type with values.
To convert a slice into an array automatically.
To delete elements from a collection.
✅ Correct!
Correct! It allows you to define the structure and the data in one step.❌ Incorrect
Composite literals are for initialization, not calculation or deletion.QUESTION 2
If an array has 8 elements (as in Figure 16.1), what is the index of the first item?
1
-1
0
8
✅ Correct!
Correct. Go uses zero-based indexing for all collections.❌ Incorrect
Collections in Go always start at index 0.QUESTION 3
What does the ellipsis (...) in a function declaration typically indicate?
The function is incomplete.
The function accepts a variable number of arguments (variadic).
The function will loop infinitely.
The function returns a pointer.
✅ Correct!
Yes, the ellipsis denotes variadic parameters.❌ Incorrect
The ellipsis is used for variadic functions, allowing them to take any number of values.QUESTION 4
In the context of the Go Cargo Bay, what are 'collections'?
Variables that can only store numbers.
Just groups of things, typically of the same type.
Special functions that sort data.
Hardware components used for storage.
✅ Correct!
Exactly. Arrays, slices, and maps are all types of collections.❌ Incorrect
Collections are logical groupings of data within the code.QUESTION 5
What happens if you attempt to access planets[8] in an array of size 8?
It returns the first element again (wrap around).
It returns a null or empty string.
It causes a compile-time error or a runtime panic.
It automatically expands the array to size 9.
✅ Correct!
Correct! Array sizes are fixed and bounds are strictly enforced.❌ Incorrect
Arrays in Go are fixed-length; you cannot access an index equal to or greater than the length.Module Challenge: The Ship's Manifest
Representing physical items as digital arrays.
You are organizing a spaceship's inventory. You have a collection of personal items like stamps, books, and trophies. You need to initialize these in Go and perform operations on them.
Q
1. Arrays are for collecting many of the same type of thing. What collections from your own life (e.g., stamps, shoes) could you represent with an array? Explain your choice.
Solution:
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Q
2. Provide the syntax for a 'Step' function signature that takes two Universe types, intended for simulation operations.
Solution:
The signature is:
The signature is:
func Step(a, b Universe). This allows the function to operate on two collections, often used for double-buffering in simulations like the Game of Life.